<?php
	include '../models/Model.php';
	$model=new Model();
	$admin=2;
	$users=1;
	$userid=1;
	$userAccName=$_POST['inputAccName'];
	$userName=$_POST['userForm$tbFamilyName'];
	$userPassword=$_POST['rpwd'];
	$userKind=$_POST['level'];
	$userTel=$_POST['userForm$tbTel'];
	$userEmail=$_POST['userForm$tbMailAddress2'];
	if($userAccName==NULL || $userPassword==NULL){
		if($userKind) header("Location:../view/manager/register.php");
		else header("Location:../view/user/register.php");
	}
	else {
		$user = $model->searchUserAccName($userAccName);
		//var_dump($user);die;
		if($user->getUserName()!=NULL) {
			echo "<script type='text/javascript'>\n";
			echo "alert('User already existed!');\n";
			if($userKind) echo "window.location='../view/manager/register.php'";
			else echo "window.location='../view/user/register.php'";
	
			echo "</script>";
		}
		else if($userKind){
			$user=new User($userid,$userAccName,$userPassword,$userKind,$userName,$userTel,$userEmail);
			$model->addUser($user);
			$date = date("Y-m-d H:i:s");
			$log = $date."---Event---".$AccName."---Registered\n";
			$path="../log/log.txt";
			$file=fopen($path, "a");
			$write=fwrite($file,$log);
			fclose($file);
			header("Location:../controller/userManagerController.php");
		}
		else {
			$user=new User($userid,$userAccName,$userPassword,0,$userName,$userTel,$userEmail);
			$model->addUser($user);
			$date = date("Y-m-d H:i:s");
			$log = $date."---Event---".$AccName."---Registered\n";
			$path="../log/log.txt";
			$file=fopen($path, "a");
			$write=fwrite($file,$log);
			fclose($file);
			echo "<script type='text/javascript'>\n";
			echo "alert('Register Success! Please wait for active !');\n";
			echo "window.location='../index.php'";
			echo "</script>";
			session_destroy();
			
			//header("Location:../controller/userManagerController.php");
		}
		}
?>